<?php
/**
 * Created by PhpStorm.
 * User: mww
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\business\controller;
use think\Db;
class Home extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
//        header('Access-Control-Allow-Origin:*');
//        extract(input());
//        if($user_id != 0){
//            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//            if($auth == 112){
//                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
//                exit;
//            }elseif($auth == 110){
//                echo json_encode(['status' => 302, 'msg' => $this->frost]);
//                exit;
//            }elseif($auth == 1103){
//                echo json_encode(['status' => 303, 'msg' => $this->del]);
//                exit;
//            }
//        }

    }

    /**
     * 卡券领取情况：
     * 数据统计
     * @return string
     */
    public function getEchartsData()
    {
        extract(input());
        if(isset($start) && $start && isset($end) && $end){
            $data = $this->getEchartsDataHaveDate($user_id,$start,$end);
        }else {
            $all = Db::table('up_shop_coupon')
                ->where('coupon_s_id', $user_id)
                ->sum('coupon_num');
            //领取的
            $all_get = Db::table('up_shop_coupon')
                ->where('coupon_s_id', $user_id)
                ->sum('coupon_get_num');
            //未领取的
            $all_not_get = $all - $all_get;

            //已核销的
            $all_use = Db::table('up_shop_coupon')
                ->where('coupon_s_id', $user_id)
                ->sum('coupon_use_num');
            //未核销的
            $all_not_use = $all_get - $all_use;

            //所有的任务
            $all_task = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->sum('task_num');
            //所有的普通任务
            $all_pt_task = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 1)
                ->sum('task_num');
            //所有的活动任务
            $all_hd_task = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 2)
                ->sum('task_num');
            //所有的达人任务
            $all_dr_task = $all_task - $all_pt_task - $all_hd_task;

            //普通已领取
            $pt_get = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 1)
                ->sum('task_get_num');
            //普通待领取
            $pt_not_get = $all_pt_task - $pt_get;
            //普通已完成审核
            $pt_use = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 1)
                ->sum('task_done_num');
            //普通待审核
            $pt_not_use = Db::table('up_task_user')
                ->where('tu_s_id', $user_id)
                ->where('tu_task_active', 1)
                ->where('tu_status', 1)
                ->count();

            //活动已领取
            $hd_get = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 2)
                ->sum('task_get_num');
            //活动待领取
            $hd_not_get = $all_hd_task - $hd_get;
            //活动已完成审核
            $hd_use = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 2)
                ->sum('task_done_num');
            //活动待审核
            $hd_not_use = Db::table('up_task_user')
                ->where('tu_s_id', $user_id)
                ->where('tu_task_active', 2)
                ->where('tu_status', 1)
                ->count();

            //活动已领取
            $dr_get = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 3)
                ->sum('task_get_num');
            //活动待领取
            $dr_not_get = $all_dr_task - $dr_get;
            //活动已完成审核
            $dr_use = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 3)
                ->sum('task_done_num');
            //活动待审核
            $dr_not_use = Db::table('up_task_user')
                ->where('tu_s_id', $user_id)
                ->where('tu_task_active', 3)
                ->where('tu_status', 1)
                ->count();

            $data = [
                'all_get' => $all_get,
                'all_not_get' => $all_not_get,

                'all_use' => $all_use,
                'all_not_use' => $all_not_use,

                'all_task' => $all_task,
                'all_pt_task' => $all_pt_task,
                'all_hd_task' => $all_hd_task,
                'all_dr_task' => $all_dr_task,

                'pt_get' => $pt_get,
                'pt_not_get' => $pt_not_get,
                'pt_use' => $pt_use,
                'pt_not_use' => $pt_not_use,

                'hd_get' => $hd_get,
                'hd_not_get' => $hd_not_get,
                'hd_use' => $hd_use,
                'hd_not_use' => $hd_not_use,

                'dr_get' => $dr_get,
                'dr_not_get' => $dr_not_get,
                'dr_use' => $dr_use,
                'dr_not_use' => $dr_not_use,
            ];
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    public function getEchartsDataHaveDate($user_id,$start,$end)
    {
        extract(input());
        $all = Db::table('up_shop_coupon')
            ->where('coupon_s_id',$user_id)
            ->whereTime('coupon_time','between',[$start,$end])
            ->sum('coupon_num');
        //领取的
        $all_get = Db::table('up_shop_coupon')
            ->where('coupon_s_id',$user_id)
            ->whereTime('coupon_time','between',[$start,$end])
            ->sum('coupon_get_num');
        //未领取的
        $all_not_get = $all - $all_get;

        //已核销的
        $all_use = Db::table('up_shop_coupon')
            ->where('coupon_s_id',$user_id)
            ->whereTime('coupon_time','between',[$start,$end])
            ->sum('coupon_use_num');
        //未核销的
        $all_not_use = $all_get - $all_use;

        //所有的任务
        $all_task = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_num');
        //所有的普通任务
        $all_pt_task = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',1)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_num');
        //所有的活动任务
        $all_hd_task = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',2)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_num');
        //所有的达人任务
        $all_dr_task = $all_task - $all_pt_task - $all_hd_task;

        //普通已领取
        $pt_get = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',1)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_get_num');
        //普通待领取
        $pt_not_get = $all_pt_task - $pt_get;
        //普通已完成审核
        $pt_use = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',1)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_done_num');
        //普通待审核
        $pt_not_use = Db::table('up_task_user')
            ->where('tu_s_id',$user_id)
            ->where('tu_task_active',1)
            ->where('tu_status',1)
            ->whereTime('tu_submit_time','between',[$start,$end])
            ->count();

        //活动已领取
        $hd_get = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',2)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_get_num');
        //活动待领取
        $hd_not_get = $all_hd_task - $hd_get;
        //活动已完成审核
        $hd_use = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',2)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_done_num');
        //活动待审核
        $hd_not_use = Db::table('up_task_user')
            ->where('tu_s_id',$user_id)
            ->where('tu_task_active',2)
            ->where('tu_status',1)
            ->whereTime('tu_submit_time','between',[$start,$end])
            ->count();

        //活动已领取
        $dr_get = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',3)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_get_num');
        //活动待领取
        $dr_not_get = $all_dr_task - $dr_get;
        //活动已完成审核
        $dr_use = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',3)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_done_num');
        //活动待审核
        $dr_not_use = Db::table('up_task_user')
            ->where('tu_s_id',$user_id)
            ->where('tu_task_active',3)
            ->where('tu_status',1)
            ->whereTime('tu_submit_time','between',[$start,$end])
            ->count();

        $data = [
            'all_get' => $all_get,
            'all_not_get' => $all_not_get,

            'all_use' => $all_use,
            'all_not_use' => $all_not_use,

            'all_task' => $all_task,
            'all_pt_task' => $all_pt_task,
            'all_hd_task' => $all_hd_task,
            'all_dr_task' => $all_dr_task,

            'pt_get' => $pt_get,
            'pt_not_get' => $pt_not_get,
            'pt_use' => $pt_use,
            'pt_not_use' => $pt_not_use,

            'hd_get' => $hd_get,
            'hd_not_get' => $hd_not_get,
            'hd_use' => $hd_use,
            'hd_not_use' => $hd_not_use,

            'dr_get' => $dr_get,
            'dr_not_get' => $dr_not_get,
            'dr_use' => $dr_use,
            'dr_not_use' => $dr_not_use,
        ];
        return $data;
    }

}